const axios = require('axios');
const config = require('../config');

class AtomGitService {
  constructor() {
    this.apiBaseUrl = 'https://api.atomgit.com';
    this.authToken = config.ATOMGIT_TOKEN;
  }

  async getPR(owner, repo, pullNumber) {
    try {
      const response = await axios({
        method: 'get',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/${pullNumber}`,
        headers: {
          'Authorization': `Bearer ${this.authToken}`
        }
      });
      return response.data;
    } catch (error) {
      console.error('Error getting PR:', error);
      throw error;
    }
  }

  async addPRComment(owner, repo, pullNumber, comment) {
    try {
      const response = await axios({
        method: 'post',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/${pullNumber}/comments`,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${this.authToken}`
        },
        data: {
          body: comment
        }
      });
      return response.data;
    } catch (error) {
      console.error('Error adding PR comment:', error);
      throw error;
    }
  }

  async getComment(owner, repo, commentId) {
    try {
      const response = await axios({
        method: 'get',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/comments/${commentId}`,
        headers: {
          'Authorization': `Bearer ${this.authToken}`
        }
      });
      return response.data;
    } catch (error) {
      console.error('Error getting comment:', error);
      throw error;
    }
  }

  async addCommentReply(owner, repo, commentId, pullNumber,reply) {
    console.log(`${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/${pullNumber}/comments/${commentId}/replies`)
    console.log('Reply:', reply)
    try {
      const response = await axios({
        method: 'post',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/${pullNumber}/comments/${commentId}/replies`,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${this.authToken}`
        },
        data: {
          body: reply
        }
      });
      console.log(response.data);
      return response.data;
    } catch (error) {
      console.error('Error adding comment reply:', error);
      throw error;
    }
  }

  async getPRComments(owner, repo, pullNumber) {
    try {
      const response = await axios({
        method: 'get',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/pulls/${pullNumber}/comments`,
        headers: {
          'Authorization': `Bearer ${this.authToken}`
        }
      });
      return response.data;
    } catch (error) {
      console.error('Error getting PR comments:', error);
      throw error;
    }
  }

  async addPRLabel(owner, repo, pullNumber, labels) {
    try {
      const response = await axios({
        method: 'post',
        url: `${this.apiBaseUrl}/repos/${owner}/${repo}/issues/${pullNumber}/labels`,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${this.authToken}`
        },
        data: labels
      });
      return response.data;
    } catch (error) {
      console.error('Error adding PR label:', error);
      throw error;
    }
  }
}

module.exports = new AtomGitService();
